package co.fitstart.admin.web.controller.courseware;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;

import co.fitstart.admin.service.GroupTemplateService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.courseware.fitnessgroup.CoursewareMovementType;
import co.fitstart.entity.courseware.fitnessgroup.CoursewareTemplate;
import co.fitstart.entity.courseware.fitnessgroup.DailyCoursewareTemplate;
import co.fitstart.entity.courseware.fitnessgroup.FitnessGroup;

/**
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping("/fitness-groups/{fitnessGroupId}/batch-courseware-templates/{weeks}/daily-courseware-templates/{dailyCoursewareTemplateId}/courseware-templates")
@MenuSetting("menu-course")
public class DailyCoursewareTemplateController {
    
    private static final Logger logger = LoggerFactory.getLogger(DailyCoursewareTemplateController.class);
    
    @Autowired
    private GroupTemplateService groupTemplateService;
    
    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @param dailyCoursewareTemplateId
     * @param type
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create(@PathVariable("fitnessGroupId") Long fitnessGroupId, 
                               @PathVariable("weeks") int weeks,
                               @PathVariable("dailyCoursewareTemplateId") Long dailyCoursewareTemplateId,
                               @RequestParam("coursewareType") String coursewareType,
                               @RequestParam(value = "type", required = false) String type){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/batch-courseware-template/courseware-template-create");
        
        FitnessGroup fitnessGroup = groupTemplateService.loadFitnessGroup(fitnessGroupId);
        
        modelAndView.addObject("fitnessGroup", fitnessGroup);
        modelAndView.addObject("weeks", weeks);
        modelAndView.addObject("dailyCoursewareTemplateId", dailyCoursewareTemplateId);
        modelAndView.addObject("coursewareType", coursewareType);
        modelAndView.addObject("type", type);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @param dailyCoursewareTemplateId
     * @param type
     * @param coursewareTemplate
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public @ResponseBody String onCreate(@PathVariable("fitnessGroupId") Long fitnessGroupId, 
                                         @PathVariable("weeks") int weeks,
                                         @PathVariable("dailyCoursewareTemplateId") Long dailyCoursewareTemplateId,
                                         @RequestParam(name = "movementType", required = false) String type,
                                         CoursewareTemplate coursewareTemplate){
        
        Result result = new Result();
        
        try{
            
            DailyCoursewareTemplate dailyCoursewareTemplate = groupTemplateService.loadDailyCoursewareTemplate(dailyCoursewareTemplateId);
            
            groupTemplateService.createCoursewareTemplate(dailyCoursewareTemplate, coursewareTemplate, CoursewareMovementType.aliasOf(type));
            
            result.success().message("添加动作成功！")
                            .data("url", "/fitness-groups/" + fitnessGroupId + "/batch-courseware-templates/" + weeks + "?type=weekly&day=" + dailyCoursewareTemplate.getSorting());
        }
        catch (Exception e) {
            
            logger.error("onCreate - ", e);
            result.fail().message("添加动作失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @param dailyCoursewareTemplateId
     * @return
     */
    @RequestMapping(value = "/action-group", method = RequestMethod.GET)
    public ModelAndView actionGroup(@PathVariable("fitnessGroupId") Long fitnessGroupId, 
                                    @PathVariable("weeks") int weeks,
                                    @PathVariable("dailyCoursewareTemplateId") Long dailyCoursewareTemplateId,
                                    @RequestParam("coursewareType") String coursewareType,
                                    @RequestParam(value = "type", required = false) String type){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/batch-courseware-template/courseware-template-action-group");
        
        modelAndView.addObject("fitnessGroupId", fitnessGroupId);
        modelAndView.addObject("weeks", weeks);
        modelAndView.addObject("dailyCoursewareTemplateId", dailyCoursewareTemplateId);
        modelAndView.addObject("coursewareType", coursewareType);
        modelAndView.addObject("type", type);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @param dailyCoursewareTemplateId
     * @param groupId
     * @return
     */
    @RequestMapping(value = "/action-group", method = RequestMethod.POST)
    public @ResponseBody String onActionGroup(@PathVariable("fitnessGroupId") Long fitnessGroupId, 
                                              @PathVariable("weeks") int weeks,
                                              @PathVariable("dailyCoursewareTemplateId") Long dailyCoursewareTemplateId,
                                              @RequestParam(name = "movementType", required = false) String type,
                                              @RequestParam("groupId") Long groupId){
        
        Result result = new Result();
        
        try{
            
            DailyCoursewareTemplate dailyCoursewareTemplate = groupTemplateService.loadDailyCoursewareTemplate(dailyCoursewareTemplateId);
            FitnessGroup fitnessGroup = groupTemplateService.loadFitnessGroup(groupId);
            
            groupTemplateService.createCoursewareGroup(dailyCoursewareTemplate, fitnessGroup, CoursewareMovementType.aliasOf(type));
            
            result.success().message("添加动作组成功！");
        }
        catch (Exception e) {
            
            logger.error("onActionGroup - ", e);
            result.fail().message("添加动作组失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @param dailyCoursewareTemplateId
     * @param coursewareTemplateId
     * @return
     */
    @RequestMapping(value = "/{coursewareTemplateId}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                             @PathVariable("weeks") int weeks,
                             @PathVariable("dailyCoursewareTemplateId") Long dailyCoursewareTemplateId,
                             @PathVariable("coursewareTemplateId") Long coursewareTemplateId){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/batch-courseware-template/courseware-template-edit");
        
        CoursewareTemplate coursewareTemplate = groupTemplateService.loadCoursewareTemplate(coursewareTemplateId);
        modelAndView.addObject("coursewareTemplate", coursewareTemplate);
        
        modelAndView.addObject("fitnessGroupId", fitnessGroupId);
        modelAndView.addObject("dailyCoursewareTemplateId", dailyCoursewareTemplateId);
        modelAndView.addObject("weeks", weeks);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @param dailyCoursewareTemplateId
     * @param coursewareTemplateId
     * @return
     */
    @RequestMapping(value = "/{coursewareTemplateId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@PathVariable("fitnessGroupId") Long fitnessGroupId, 
                                       @PathVariable("weeks") int weeks,
                                       @PathVariable("dailyCoursewareTemplateId") Long dailyCoursewareTemplateId,
                                       @Preloading("coursewareTemplateId") CoursewareTemplate coursewareTemplate){
        
        Result result = new Result();
        
        try{
            
            groupTemplateService.editCoursewareTemplate(coursewareTemplate);
            
            result.success().message("修改动作成功！")
                            .data("url", "/fitness-groups/" + fitnessGroupId + "/batch-courseware-templates/" + weeks + "?type=weekly&day=" + coursewareTemplate.getDaily().getSorting());
        }
        catch (Exception e) {
            
            logger.error("onEdit - ", e);
            result.fail().message("修改动作失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    
    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @param dailyCoursewareTemplateId
     * @param coursewareTemplateId
     * @return
     */
    @RequestMapping(value = "/{coursewareTemplateId}/delete", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                               @PathVariable("weeks") int weeks,
                               @PathVariable("dailyCoursewareTemplateId") Long dailyCoursewareTemplateId,
                               @PathVariable("coursewareTemplateId") Long coursewareTemplateId){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/batch-courseware-template/courseware-template-delete");
        
        modelAndView.addObject("fitnessGroupId", fitnessGroupId);
        modelAndView.addObject("weeks", weeks);
        modelAndView.addObject("dailyCoursewareTemplateId", dailyCoursewareTemplateId);
        modelAndView.addObject("coursewareTemplateId", coursewareTemplateId);
        
        return modelAndView;
    }
    
    

    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @param dailyCoursewareTemplateId
     * @param coursewareTemplateId
     * @return
     */
    @RequestMapping(value = "/{coursewareTemplateId}", method = RequestMethod.DELETE)
    public @ResponseBody String onDelete(@PathVariable("fitnessGroupId") Long fitnessGroupId, 
                                         @PathVariable("weeks") int weeks,
                                         @PathVariable("dailyCoursewareTemplateId") Long dailyCoursewareTemplateId,
                                         @Preloading("id") CoursewareTemplate coursewareTemplate){
        
        Result result = new Result();
        
        try{
            
            groupTemplateService.deleteCoursewareTemplate(coursewareTemplate);
            
            result.success().message("删除动作成功！")
                            .data("url", "/fitness-groups/" + fitnessGroupId + "/batch-courseware-templates/" + weeks + "?type=weekly&day=" + coursewareTemplate.getDaily().getSorting());
        }
        catch (Exception e) {
            
            logger.error("onDelete - ", e);
            result.fail().message("删除动作失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }

    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @param dailyCoursewareTemplateId
     * @param coursewareTemplateId
     * @return
     */
    @RequestMapping(value = "/{coursewareTemplateId}/rise", method = RequestMethod.GET)
    public ModelAndView riseRanking(@PathVariable("fitnessGroupId") Long fitnessGroupId, 
                                    @PathVariable("weeks") int weeks,
                                    @PathVariable("dailyCoursewareTemplateId") Long dailyCoursewareTemplateId,
                                    @PathVariable("coursewareTemplateId") Long coursewareTemplateId){
        
        ModelAndView modelAndView = new ModelAndView();
        
        CoursewareTemplate coursewareTemplate = groupTemplateService.loadCoursewareTemplate(coursewareTemplateId);
        
        groupTemplateService.riseRanking(coursewareTemplate);
        
        modelAndView.setViewName("redirect:/fitness-groups/" + fitnessGroupId + "/batch-courseware-templates/" + weeks + "?type=weekly&day=" + coursewareTemplate.getDaily().getSorting());
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @param dailyCoursewareTemplateId
     * @param coursewareTemplateId
     * @return
     */
    @RequestMapping(value = "/{coursewareTemplateId}/drop", method = RequestMethod.GET)
    public ModelAndView dropRanking(@PathVariable("fitnessGroupId") Long fitnessGroupId, 
                                    @PathVariable("weeks") int weeks,
                                    @PathVariable("dailyCoursewareTemplateId") Long dailyCoursewareTemplateId,
                                    @PathVariable("coursewareTemplateId") Long coursewareTemplateId){
        
        ModelAndView modelAndView = new ModelAndView();
        
        CoursewareTemplate coursewareTemplate = groupTemplateService.loadCoursewareTemplate(coursewareTemplateId);
        
        groupTemplateService.dropRanking(coursewareTemplate);
        
        modelAndView.setViewName("redirect:/fitness-groups/" + fitnessGroupId + "/batch-courseware-templates/" + weeks + "?type=weekly&day=" + coursewareTemplate.getDaily().getSorting());
        
        return modelAndView;
    }
}
